package com.ruoyi.web.controller.campus;

import java.util.*;

import com.alibaba.fastjson.JSON;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.campus.domain.MyCommodity;
import com.ruoyi.campus.service.IMyCommodityService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 商品信息Controller
 *
 * @author zht
 * @date 2021-10-24
 */
@RestController
@RequestMapping("/campus/commodity")
public class MyCommodityController extends BaseController
{
    @Autowired
    private IMyCommodityService myCommodityService;

    /**
     * 查询商品信息列表
     */
    @PreAuthorize("@ss.hasPermi('campus:commodity:list')")
    @GetMapping("/list")
    public TableDataInfo list(MyCommodity myCommodity)
    {
        startPage();
        List<MyCommodity> list = myCommodityService.selectMyCommodityList(myCommodity);
        return getDataTable(list);
    }

    /**
     * 查询商品(小程序端接口)
     */
    @PreAuthorize("@ss.hasAnyRoles('Administrator,member')")
    @GetMapping("/listForWx")
    public TableDataInfo listForWx(MyCommodity myCommodity)
    {
        startPage();
        if (!"".equals(myCommodity.getTypes()) && StringUtils.isNotNull(myCommodity.getTypes())){
            List<String> asList = Arrays.asList(myCommodity.getTypes().split(","));
            myCommodity.setParams(new HashMap<String, Object>(){{
                put("types",asList);
            }});
        }
        myCommodity.setComStatus("1");
        List<MyCommodity> list = myCommodityService.selectMyCommodityList(myCommodity);
        return getDataTable(list);
    }

    /**
     * 根据comId获取商品详情信息(小程序端接口)
     */
    @PreAuthorize("@ss.hasAnyRoles('Administrator,member')")
    @GetMapping("/getDetailByComId")
    public Map<String,Object> getDetailByComId(@RequestParam("comId") String comId){
        Map<String, Object> detail = myCommodityService.getDetailByComId(comId);
        String imgUrl = detail.get("imgUrl").toString();
        String[] split = imgUrl.split(",");
        List<Map<String,String>> map = new ArrayList<Map<String,String>>();
        for (String s : split) {
            map.add(new HashMap<String,String>(){{
                put("image","http://localhost:8088"+s);
            }});
        }
        detail.put("imgUrl",JSON.toJSON(map));
        return detail;
    }

    /**
     * 查询个人闲置(小程序端接口)
     */
    @PreAuthorize("@ss.hasAnyRoles('Administrator,member')")
    @GetMapping("/selectMyCommodity")
    public TableDataInfo selectMyCommodity(@RequestParam("userId") String userId,@RequestParam("comStatus") String comStatus){
        startPage();
        List<Map<String,Object>> list = myCommodityService.selectMyCommodity(userId,comStatus);
        for (Map<String, Object> map : list) {
            String soldMoney = map.get("soldMoney").toString();
            String oriMoney = map.get("oriMoney").toString();
            map.put("soldMoney",soldMoney);
            map.put("oriMoney",oriMoney);
        }
        return getDataTable(list);
    }

    /**
     * 导出商品信息列表
     */
    @PreAuthorize("@ss.hasPermi('campus:commodity:export')")
    @Log(title = "商品信息", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(MyCommodity myCommodity)
    {
        List<MyCommodity> list = myCommodityService.selectMyCommodityList(myCommodity);
        ExcelUtil<MyCommodity> util = new ExcelUtil<MyCommodity>(MyCommodity.class);
        return util.exportExcel(list, "商品信息数据");
    }

    /**
     * 获取商品信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('campus:commodity:query')")
    @GetMapping(value = "/{comId}")
    public AjaxResult getInfo(@PathVariable("comId") String comId)
    {
        return AjaxResult.success(myCommodityService.selectMyCommodityByComId(comId));
    }

    /**
     * 新增商品信息
     */
    @PreAuthorize("@ss.hasPermi('campus:commodity:add')")
    @Log(title = "商品信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody MyCommodity myCommodity)
    {
        myCommodity.setComId(UUID.randomUUID().toString().replace("-",""));
        return toAjax(myCommodityService.insertMyCommodity(myCommodity));
    }

    /**
     * 新增商品信息(小程序端接口)
     */
    @PreAuthorize("@ss.hasAnyRoles('Administrator,member')")
    @Log(title = "商品信息", businessType = BusinessType.INSERT)
    @PostMapping("/addForWx")
    public AjaxResult addForWx(@RequestBody MyCommodity myCommodity)
    {
        myCommodity.setComStatus("3");
        myCommodity.setComId(UUID.randomUUID().toString().replace("-",""));
        return toAjax(myCommodityService.insertMyCommodity(myCommodity));
    }

    /**
     * 修改商品信息
     */
    @PreAuthorize("@ss.hasPermi('campus:commodity:edit')")
    @Log(title = "商品信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody MyCommodity myCommodity)
    {
        return toAjax(myCommodityService.updateMyCommodity(myCommodity));
    }

    /**
     * 删除商品信息
     */
    @PreAuthorize("@ss.hasPermi('campus:commodity:remove')")
    @Log(title = "商品信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{comIds}")
    public AjaxResult remove(@PathVariable String[] comIds)
    {
        return toAjax(myCommodityService.deleteMyCommodityByComIds(comIds));
    }

    /**
     * 根据typeId得到商品类别
     */
    @GetMapping(value = "/getType/{typeId}")
    public AjaxResult getType(@PathVariable("typeId") String typeId)
    {
        return AjaxResult.success(myCommodityService.selectCommTypeById(typeId));
    }

    /**
     * 根据typeName得到商品类别
     */
    @GetMapping(value = "/getCommTypeByTypeNameLike/{typeName}")
    public AjaxResult getCommTypeByTypeNameLike(@PathVariable("typeName") String typeName)
    {
        return AjaxResult.success(myCommodityService.getCommTypeByTypeNameLike(typeName));
    }
}
